package com.tuanzi.loan.web.bean.pageTemplate;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.common.CommProductId;
import com.tuanzi.loan.business.service.common.CommProductIdService;
import com.tuanzi.loan.business.service.pageTemplate.LoanPageTemplateInfoService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.pageTemplate.LoanPageTemplateInfoRequest;
import com.tuanzi.loan.vo.response.pageTemplate.LoanPageTemplateInfoResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.pageTemplate.LoanPageTemplateInfoLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.stream.Collectors;

import static com.tuanzi.loan.business.constants.DictionaryCode.EXPORT_TYPE;
import static com.tuanzi.loan.business.constants.DictionaryCode.LARGE_COMMON_PRODUCT;
import static com.tuanzi.loan.business.constants.DictionaryCode.ONLINE_OFFLINE_STATUS;

/**
 * @Auther: hequnayu
 * @Date: 2019/9/17 11:32
 * @Description:
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanPageTemplateListBean extends JSFListingBean<LoanPageTemplateInfoRequest,LoanPageTemplateInfoResponse> {
    private static final long serialVersionUID = 5468782554288796833L;

    @Autowired
    private LoanPageTemplateInfoService templateInfoService;
    @Autowired
    private CommProductIdService commProductIdService;

    private List<CommProductId> commProductIds;

    @PostConstruct
    private void init() {
        commProductIds = commProductIdService.findAll().stream().filter(x -> x.getRealPrdid() != null).collect(Collectors.toList());
        initLazyDataModel();
    }

    private void initLazyDataModel() {
        $ = new LoanPageTemplateInfoRequest();
        lazyDataModel = new LoanPageTemplateInfoLazyDataModel($);
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onSelectParentProd(){
        if (StringUtils.isNotEmpty($.getParentId())){
            commProductIds = commProductIdService.findAllByParentId($.getParentId()).stream().filter(x -> x.getRealPrdid() != null).collect(Collectors.toList());
        }
    }

    public String onAdd() {
        return redirect("/views/pageTemplate/info.xhtml");
    }

    public String onEdit(String id) {
        return redirect("/views/pageTemplate/info.xhtml?id=" + id);
    }

    public void online(Long pageTemplateId){
        templateInfoService.online(templateInfoService.findOne(pageTemplateId));
        addSuccessfullyMessage();
    }

    public void offline(Long pageTemplateId){
        templateInfoService.offline(templateInfoService.findOne(pageTemplateId));
        addSuccessfullyMessage();
    }

    public void onDelete(Long pageTemplateId){
        templateInfoService.deleteTemplate(templateInfoService.findOne(pageTemplateId));
        addSuccessfullyMessage();
    }


    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(LARGE_COMMON_PRODUCT,ONLINE_OFFLINE_STATUS);
    }
}
